package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ListUtil;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.ProductInquiry;
import com.cz.zslu.entity.ProductQuote;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.ZslStoreCommodity;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IZslStoreCommodityService;
import com.cz.zslu.service.ProductInquiryService;
import com.cz.zslu.service.ProductQuoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 报价管理 前端控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-06-20
 */
@RestController
@RequestMapping("/sys/quote")
@Api(description = "报价管理")
public class ProductQuoteController extends BaseController<ProductQuoteService, ProductQuote> {

    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ProductQuoteService productQuoteService;
    @Autowired
    private ProductInquiryService productInquiryService;
    @Autowired
    private IZslStoreCommodityService iZslStoreCommodityService;


    @ApiOperation("获取我的报价动态分页")
//    @IgnoreAuth
    @RequestMapping(value = "/getMyQuotePage", method = RequestMethod.POST)
    public ReturnObject<List<ProductInquiry>> getMyQuotePage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                           @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                           @ApiParam("类型（1、未报价，0、已报价）：") @RequestParam Integer type) {

        EntityWrapper<ProductInquiry> piEw = new EntityWrapper<>();
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        List<Long> yetList =new ArrayList<Long>();
        List<Long> allList =new ArrayList<Long>();
        List<Long> remainList =new ArrayList<Long>();
        //已报价， quoter_id是自己
        EntityWrapper<ProductQuote> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.eq("quoter_id", sysUser.getId());
        ew.orderBy("crt_time", false);
        List<ProductQuote> productQuotes=baseService.selectList(ew);
        if(productQuotes!=null&& productQuotes.size()>0){
           for(ProductQuote productQuote:productQuotes){
               yetList.add(productQuote.getProductInquiryId());
           }
        }
//        //未报价 quoter_id不是自己
//        List<ProductInquiry> allProductInquiry= productInquiryService.selectList(new EntityWrapper<ProductInquiry>());
//        if(allProductInquiry!=null&&allProductInquiry.size()>0){
//            for (int i = 0; i <allProductInquiry.size() ; i++) {
//                allList.add(allProductInquiry.get(i).getId());
//            }
//        }
//        for (int j = 0; j < yetList.size(); j++) {
//          for (int i = 0; i <allList.size() ; i++) {
//                if(allList.get(i).longValue()==yetList.get(j).longValue()){
//                    allList.remove(i);
//                }
//            }
//        }
//        remainList=allList;
        if(type==0){
            piEw.in("id",yetList);
        }else{
            piEw.notIn("id",yetList);
        }

        piEw.eq("state",0);
        piEw.ne("inquirer_id",BaseContextHandler.getUserID());
        Page<ProductInquiry> obj = productInquiryService.selectPage(new Page<>(offset, limit), piEw);
        if (obj.getRecords() != null && obj.getRecords().size() > 0) {

            for (ProductInquiry productInquiry : obj.getRecords()) {
                productInquiry.setProductQuotes(productQuoteService.selectList(new EntityWrapper<ProductQuote>().eq("product_inquiry_id",productInquiry.getId())));
            }
        }


        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("获取详情")
//    @IgnoreAuth
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<ProductQuote> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "获取成功", baseService.selectById(id));
    }


    @ApiOperation("报价商品")
    @RequestMapping(value = "/quoteProduct", method = RequestMethod.POST)
    public ReturnObject quoteProduct(@ApiParam("询价配件id") @RequestParam Long productInquiryId,
                                     @ApiParam("报价价格") @RequestParam BigDecimal quotePrice,
                                     @ApiParam("商品id") @RequestParam Long commondityId,
                                     @ApiParam("商品名称") @RequestParam String commodityName
    ) {

        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        ProductQuote info = new ProductQuote();
        info.setQuotePrice(quotePrice);
        info.setCommondityId(commondityId);
        info.setCommodityName(commodityName);
        info.setQuoterId(sysUser.getId());
        info.setProductInquiryId(productInquiryId);
        productQuoteService.insert(info);
        ProductInquiry productInquiry = productInquiryService.selectById(productInquiryId);
        productInquiry.setType(1);
        productInquiry.setNumber(productInquiry.getNumber()+1);
        productInquiryService.updateById(productInquiry);
        return new ReturnObject<>(0, "报价成功");
    }


    @ApiOperation("获取我的商品")
    @RequestMapping(value = "/getMyProductPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getMyProductPage() {
        List<ZslStoreCommodity> list=iZslStoreCommodityService.selectList(new EntityWrapper<ZslStoreCommodity>().eq("state", 0).eq("status", 0).eq("user_id", BaseContextHandler.getUserID()));
        return new ReturnObject<>(0, "获取成功",list);
    }

}

